package com.mshop.fw.util;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * 
 * Title:
 * 
 * Description:
 * 
 * Copyright: Copyright (c) 2008
 * 
 * @author haoxz11
 * @created Mar 7, 2008 1:37:02 PM
 * @version $Id: RegexUtil.java,v 1.1 2008/03/26 06:24:07 airsou Exp $
 */
public class RegexUtil {
	@SuppressWarnings("unchecked")
	public static ArrayList<String[]> getListMatcher(String str, String regex) {		 
		ArrayList<String[]> list=new ArrayList();
		Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(str);
		while(matcher.find()){
			int i=matcher.groupCount();
			String[] s=new String[i];
			for(int j=1;j<(i+1);j++)s[j-1]=matcher.group(j);
			list.add(s);
		}
		return list;
	}
	
	public static String replaceMatcher(String str, String regex,String tostr) {
		Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(str);
		StringBuffer buf = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(buf,tostr);
		}
		matcher.appendTail(buf);
		return buf.toString();
	}
	
	public static String getHiddenInputRegex(){
		return "<input[^>]+type=\"hidden\"[^>]+name=\"([^\"]+)\"[^>]+value=\"([^\"]*)\"[^>]*>";
	}
	
	public static String replaceBlankSpan(String str){
		return RegexUtil.replaceMatcher(str,"<SPAN style=\"FONT-SIZE: 0pt; COLOR: #ffffff\"[^>]*>[^<]*</SPAN>","");
	}
	
	public static String toLink(String content){
		content=RegexUtil.replaceMatcher(content, "((?:(?:http|ftp|https):\\/\\/){0,1}(?:(?:\\w|\\d)+\\.)+(?:\\S+))","<a href=\"$1\">$1</a>");
		return content;
	}
}
